package com.example.firstapp;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewActivity extends AppCompatActivity {

    private RecyclerView listRv;
    private final String[] names = {"小猫","哈士奇","小黄鸭","小鹿","老虎"};
    private final int[] icons = { R.drawable.cat, R.drawable.siberiankusky, R.drawable.yellowduck,
            R.drawable.fawn, R.drawable.tiger};
    private final String[] introduces = {
            "猫，属于猫科动物，分家猫、野猫，是全世界家庭中较为广泛的宠物。",
            "西伯利亚雪橇犬，常见别名哈士奇，呢称二哈",
            "鸭的体型相对较小，颈短，一些属的嘴要大些。腿位于身体后方，因而步态蹒跚。",
            "鹿科是哺乳纲偶蹄目下的一科动物，体型大小不等，为有角的反刍类。",
            "虎，大型猫科动物：毛色浅黄或棕黄色，满有黑色横纹；头圆、耳短，耳背面黑色，中央有一斑甚显著；四肢健壮有力；尾粗长，具黑色环纹，尾端黑色"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        listRv = findViewById(R.id.rv_data);
        // 设置方向(默认垂直)
        listRv.setLayoutManager(new LinearLayoutManager(this));
        MyAdapter myAdapter = new MyAdapter();
        listRv.setAdapter(myAdapter);

        // 设置点击事件
        onItemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(RecyclerViewActivity.this, "点击了"+names[position], Toast.LENGTH_SHORT).show();
            }
        };
    }

    // 创建adapter
    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

        // 创建ViewHolder
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            // 加载布局
            View view = View.inflate(RecyclerViewActivity.this, R.layout.rv_item_list, null);
            return new ViewHolder(view);
        }

        // 绑定ViewHolder
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.iconIv.setImageResource(icons[position]);
            holder.nameTv.setText(names[position]);
            holder.introduceTv.setText(introduces[position]);

            // 添加点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null){
                        onItemClickListener.onItemClick(position);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return names.length;
        }

        class ViewHolder extends RecyclerView.ViewHolder{
            ImageView iconIv;
            TextView nameTv, introduceTv;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                iconIv = itemView.findViewById(R.id.rv_iv);
                nameTv = itemView.findViewById(R.id.tv_name);
                introduceTv = itemView.findViewById(R.id.tv_introduce);
            }
        }
    }

    private OnItemClickListener onItemClickListener;

    // 定义接口事件
    interface OnItemClickListener{
        void onItemClick(int position);
    }
}